#!/bin/bash

while true; do
  CURRENT_HOUR=$(date +%H)
  CURRENT_MIN=$(date +%M)
  CURRENT_TIME=$(date "+%H:%M")
  
  # 判断当前是白天(8:00-21:00)还是夜间
  if [ "$CURRENT_HOUR" -ge 09 ] && [ "$CURRENT_HOUR" -lt 21 ]; then
    PERIOD="白天"
    echo $CURRENT_TIME
    kubectl patch hpa meetcha-prod-java-gateway-v1 -n meetcha-prod --type='json' -p="[{\"op\":\"replace\",\"path\":\"/spec/minReplicas\",\"value\":1}]"
    sleep 1
    kubectl patch hpa meetcha-prod-java-chat-v1 -n meetcha-prod --type='json' -p="[{\"op\":\"replace\",\"path\":\"/spec/minReplicas\",\"value\":2}]"
    sleep 1
    kubectl patch hpa meetcha-prod-java-game-v1 -n meetcha-prod --type='json' -p="[{\"op\":\"replace\",\"path\":\"/spec/minReplicas\",\"value\":1}]"
    sleep 1
    kubectl patch hpa meetcha-prod-java-user-v1 -n meetcha-prod --type='json' -p="[{\"op\":\"replace\",\"path\":\"/spec/minReplicas\",\"value\":2}]"
  else
    PERIOD="夜间"
    echo $CURRENT_TIME
    kubectl patch hpa meetcha-prod-java-gateway-v1 -n meetcha-prod --type='json' -p="[{\"op\":\"replace\",\"path\":\"/spec/minReplicas\",\"value\":3}]"
    sleep 1
    kubectl patch hpa meetcha-prod-java-chat-v1 -n meetcha-prod --type='json' -p="[{\"op\":\"replace\",\"path\":\"/spec/minReplicas\",\"value\":3}]"
    sleep 1
    kubectl patch hpa meetcha-prod-java-game-v1 -n meetcha-prod --type='json' -p="[{\"op\":\"replace\",\"path\":\"/spec/minReplicas\",\"value\":2}]"
    sleep 1
    kubectl patch hpa meetcha-prod-java-user-v1 -n meetcha-prod --type='json' -p="[{\"op\":\"replace\",\"path\":\"/spec/minReplicas\",\"value\":3}]"
  fi
  
  # 计算到下一个检查点的时间(每分钟检查一次)
  sleep 600
done